package crew.blt.handsound.client.mvc.view;

import com.google.gwt.core.client.GWT;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiTemplate;
import com.google.gwt.user.client.Command;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.MenuBar;
import com.google.gwt.user.client.ui.MenuItem;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.Widget;

import crew.blt.handsound.client.mvc.controller.IMainController;
import crew.blt.handsound.client.mvc.model.Model;
import crew.blt.handsound.client.mvc.view.crud.user.EditUserPage;
import crew.blt.handsound.client.mvc.view.crud.user.UserPage;
import crew.blt.handsound.client.mvc.view.home.HomePage;
import crew.blt.handsound.shared.domain.Auth;
import crew.blt.handsound.shared.domain.AuthType;
import crew.blt.handsound.shared.domain.User;


public class MainView <M extends Model,C extends IMainController> extends Composite implements IMainView<M,C>{
	
	@UiTemplate("MainView.ui.xml")
	interface MainViewUIBinder extends UiBinder<Widget, MainView> {}
	private static MainViewUIBinder uiBinder = GWT.create(MainViewUIBinder.class);
	
	private final C controller;
	private final M model;
	private IPage currentPage = new HomePage<Model>();
	
	@UiField MenuItem menuPanelHomeBt;
	//
	@UiField SimplePanel panel;

	//
	@UiField MenuItem menuPanelUser;
	@UiField MenuItem menuPanelEditUserBt;
	@UiField MenuItem menuPanelListUserBt;
	//
	@UiField MenuItem loggedUsername;
	@UiField MenuBar loggedAuthBar;
	@UiField MenuItem loggedLogoutBt;
	
	public MainView(M m,C c) {
		this.model = m;
		this.controller = c;
		initWidget(uiBinder.createAndBindUi(this));
		initGui();
	}

	private void initGui() {
		//
		menuPanelHomeBt.setCommand(onHomeBtClick);
		//Entity

		//Logout
		loggedLogoutBt.setCommand(logoutBtClick);

		//Edit
		menuPanelEditUserBt.setCommand(onEditUserBtClick);
		menuPanelListUserBt.setCommand(onListUserBtClick);
		
		RootPanel.get("PagePanel").add(this);
	}
	

	//---------------------------------- UI action
	
	private void addAdminView() {
		boolean contains = true;
		menuPanelUser.setVisible(contains);
		//
		menuPanelEditUserBt.setVisible(contains);
		menuPanelListUserBt.setVisible(contains);
	}
	
	private void addUserView() {
		boolean contains = true;
	}
	
	private void setNoAuthView() {
		boolean contains = false;
		menuPanelUser.setVisible(contains);
		//
		menuPanelEditUserBt.setVisible(contains);
		menuPanelListUserBt.setVisible(contains);
	}
	
	//---------------------------------- Event

	@Override
	public void onUserChange(User user) {
		if(user == null)
			return;
		if(user.getAuths().contains(new Auth(model.getUser(),AuthType.ROLE_OPERATOR))){
			addUserView();
			loggedAuthBar.addItem(new MenuItem("Operator", false, new Command() {
				@Override
				public void execute() {
				}
			}));
		}
		if(user.getAuths().contains(new Auth(model.getUser(),AuthType.ROLE_ADMIN))){
			addAdminView();
			loggedAuthBar.addItem(new MenuItem("Admin", false, new Command() {
				@Override
				public void execute() {
				}
			}));
		}
		loggedUsername.setText(""+user.getUsername());
		currentPage.setUser(user);
	}

	@Override
	public void onUserChange() {
		onUserChange(model.getUser());
	}
	
	//---------------------------------- Interface behavior
	
	@Override
	public void setHomePage(M model) {
		currentPage = new HomePage<M>();
		panel.setWidget(currentPage);
		currentPage.setUser(model.getUser());
	}

	@Override
	public void setEditUserPage(M model) {
		currentPage = new EditUserPage<M>();
		panel.setWidget(currentPage);
		currentPage.setUser(model.getUser());
	}
	
	@Override
	public void setListUserPage(M model) {
		currentPage = new UserPage<M>();
		panel.setWidget(currentPage);
		currentPage.setUser(model.getUser());
	}
	//--------------------------------------Ui Handler
	
	private final Command onHomeBtClick = new Command() {
		@Override
		public void execute() {
			controller.onPageRequest(SitePage.HOME);
		}};

	//-------------------------- User pages
	private final Command onEditUserBtClick = new Command() {
		@Override
		public void execute() {
			controller.onPageRequest(SitePage.EDIT_USER);
		}};

	private final Command onListUserBtClick = new Command() {
		@Override
		public void execute() {
			controller.onPageRequest(SitePage.USER);
		}};
		
	private final Command logoutBtClick = new Command() {
		@Override
		public void execute() {
			controller.onPageRequest(SitePage.LOGOUT);
		}};
}
